package com.campus.mapper.Impl;

import com.campus.domain.Dto.ClassCreateDto;
import com.campus.domain.Dto.ClassUpdateDto;
import com.campus.mapper.ClassMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.sql.Date;

@Repository
public class ClassMapperImpl implements ClassMapper {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public Integer addClass(ClassCreateDto classCreateDto) {
        String sql = "Insert into t_manager_class (name,created_time,class_desc,activity_id) values (?,?,?,?)";

        Object[] args= new Object[]{
                classCreateDto.getName(),
                new Date(new java.util.Date().getTime()),
                classCreateDto.getClassDesc(),
                classCreateDto.getActivityId()

        };

        return jdbcTemplate.update(sql,args);
    }

    @Override
    public Integer updateClass(ClassUpdateDto classUpdateDto) {
        String sql = "update t_manager_class set name = ?, class_desc = ? where id = ?";

        Object[] args = new Object[]{
                classUpdateDto.getName(),
                classUpdateDto.getClassDesc(),
                classUpdateDto.getId()
        };

        return jdbcTemplate.update(sql,args);
    }

    @Override
    public Integer allocateTeacher(Long classId, Long teacherId) {
        String sql = "update t_manager_class set teacher_id = ? where id = ?";

        Object[] args = new Object[]{
                classId,
                teacherId
        };

        return jdbcTemplate.update(sql,args);
    }

    @Override
    public Integer addStudentInClass(Long classId, Long studentId) {
        String sql = "insert into t_teaching_student_in_class (class_id, student_id) values (?,?)";

        Object[] args = new Object[]{
                classId,
                studentId
        };

        return jdbcTemplate.update(sql,args);
    }
}
